
import Api from '@/database'
import { defineWrappedResponseHandler } from '../utils/handler'

const handler = defineEventHandler(async (e) => {
  const { table, pager } = await readBody(e)
  const columns = await Api.table.columns(table)
  const primaryKey = Api.base.showPrimaryKey()
  const result = await new Api.Entity(table).page(pager)
  const cols = columns as string[]
  return {
    ...result,
    primaryKey,
    columns: cols.map((k: any) => {
      return {
        prop: k.field, 
        label: k.field,
        type: k.type
      }
    })
  }
})

export default defineWrappedResponseHandler(handler)